﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.DynamicData;
using System.Web.UI;

namespace NotAClue.Web.DynamicData
{
    public static class DynamicControlExtensionMethods
    {
        /// <summary>
        /// Get the control by searching recursively for it.
        /// </summary>
        /// <param name="Root">The control to start the search at.</param>
        /// <param name="Id">The ID of the control to find</param>
        /// <returns>The control the was found or NULL if not found</returns>
        public static DynamicControl FindDynamicControlRecursive(this Control root, string dataField)
        {
            var dc = root as DynamicControl;
            if (dc != null)
            {
                if (String.Compare(dc.DataField, dataField, true) == 0)
                    return dc;
            }

            foreach (Control Ctl in root.Controls)
            {
                var FoundCtl = Ctl.FindDynamicControlRecursive(dataField);

                if (FoundCtl != null)
                    return FoundCtl;
            }
            return null;
        }
    }
}
